package com.avcdata.national.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avcdata.aoscustommade.R;
import com.avcdata.national.utils.DrawHorizontalListView;

import java.util.List;
import java.util.Map;

public class PriceAdapter extends BaseAdapter {
    private Activity context;
    private List<String> list;
    private Map<String,List<String>> map;
    private boolean isTop = true;

    public PriceAdapter(Activity context, List<String> list,Map<String,List<String>> map,boolean isTop) {
        this.context = context;
        this.list = list;
        this.map = map;
        this.isTop = isTop;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View converView, ViewGroup arg2) {
        ViewHolder holder;
        if (converView == null) {
            converView = LayoutInflater.from(context).inflate(R.layout.item_national_structure, null);
            holder = new ViewHolder();
            holder.roots = (LinearLayout) converView.findViewById(R.id.roots);
            converView.setTag(holder);
        } else {
            holder = (ViewHolder) converView.getTag();
        }
        if(!(list.get(position).equals("A.O.史密斯"))){
            if(isTop){
                final DrawHorizontalListView root = new DrawHorizontalListView ( context, dip2px ( context, 1 ), map.get(list.get(position)+"_market").toArray(new String[map.get(list.get(position)+"_market").size()]), 3 ,true);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );
                final DrawHorizontalListView root1 = new DrawHorizontalListView ( context, dip2px ( context, 1 ), map.get(list.get(position)+"_ratios").toArray(new String[map.get(list.get(position)+"_ratios").size()]), 3 ,false );
                LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );
                root.invalidate ();
                root1.invalidate ();
                holder.roots.removeAllViews ();
                holder.roots.addView ( root, params );
                holder.roots.addView ( root1, params1 );
            }else{
                final DrawHorizontalListView root = new DrawHorizontalListView ( context, dip2px ( context, 1 ), map.get(list.get(position)+"_prices").toArray(new String[map.get(list.get(position)+"_prices").size()]), 3 ,true);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );
                final DrawHorizontalListView root1 = new DrawHorizontalListView ( context, dip2px ( context, 1 ), map.get(list.get(position)+"_pRatios").toArray(new String[map.get(list.get(position)+"_pRatios").size()]), 3,false );
                LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );
                root.invalidate ();
                root1.invalidate ();
                holder.roots.removeAllViews ();
                holder.roots.addView ( root, params );
                holder.roots.addView ( root1, params1 );
            }
        }
        return converView;
    }

    class ViewHolder {
        LinearLayout roots;
    }

    /**
     * 根据手机分辨率从dp转到px
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources ().getDisplayMetrics ().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
